package bb.lanxing.calc.fixer;

import com.github.mikephil.charting.utils.Utils;
import bb.lanxing.calc.data.SourcePoint;
import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.manager.SPConstant;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.DistanceUtil;
import bb.lanxing.util.Log;

public class SpeedFixer {
    final static private String TAG = "SpeedFixer";
    private int errorTimes;
    private SourcePoint lastSourcePoint;

    private double h2s(double kmh) {
        return kmh / 3.6d;
    }

    private double s2h(double ms) {
        return ms * 3.6d;
    }

    public void fix(SourcePoint sourcePoint) {
        SourcePoint sourcePoint2 = this.lastSourcePoint;
        if (sourcePoint2 != null) {
            double speed = sourcePoint2.getSpeed();
            double d = Utils.DOUBLE_EPSILON;
            if (speed != Utils.DOUBLE_EPSILON) {
                double timestamp = (sourcePoint.getTimestamp() - this.lastSourcePoint.getTimestamp()) / 1000.0d;
                double speed2 = this.lastSourcePoint.getSpeed();
                double speed3 = sourcePoint.getSpeed();
                if (timestamp != Utils.DOUBLE_EPSILON) {
                    d = (speed3 - speed2) / timestamp;
                }
                if (!isSpeedValid(speed2, d)) {
                    int i = this.errorTimes + 1;
                    this.errorTimes = i;
                    if (i < 10) {
                        sourcePoint.setSpeed(this.lastSourcePoint.getSpeed());
                        return;
                    }
                }
                this.errorTimes = 0;
                this.lastSourcePoint = sourcePoint;
                return;
            }
        }
        if (sourcePoint.getSpeed() > h2s(14.4d)) {
            sourcePoint.setSpeed(h2s(14.4d));
        }
        this.lastSourcePoint = sourcePoint;
    }

    public void fix2(SourcePoint sourcePoint) {
        SourcePoint sourcePoint2 = this.lastSourcePoint;
        if (sourcePoint2 == null || sourcePoint2.getSpeed() == Utils.DOUBLE_EPSILON) {
            if (sourcePoint.getSpeed() > h2s(10.0d)) {
                sourcePoint.setSpeed(h2s(10.0d));
            }
            this.lastSourcePoint = sourcePoint;
            return;
        }
        double timestamp = (sourcePoint.getTimestamp() - this.lastSourcePoint.getTimestamp()) / 1000.0d;
        double speed = this.lastSourcePoint.getSpeed();
        double speed2 = sourcePoint.getSpeed();
        double d = timestamp == Utils.DOUBLE_EPSILON ? 0.0d : (speed2 - speed) / timestamp;
        Log.v(TAG, "lastSpeed = " + speed + ", thisSpeed = " + speed2 + ", seconds = " + timestamp + ", accSpeed = " + d);
        if (d > Utils.DOUBLE_EPSILON) {
            double maxAcceleratedSpeed = getMaxAcceleratedSpeed(speed);
            if (maxAcceleratedSpeed > Utils.DOUBLE_EPSILON && d > maxAcceleratedSpeed) {
                if (timestamp < 5.0d) {
                    double d2 = speed + (timestamp * maxAcceleratedSpeed);
                    Log.v(TAG, "maxAccSpeed = " + maxAcceleratedSpeed + " fixedSpeed = " + d2);
                    sourcePoint.setSpeed(d2);
                } else {
                    double calAvgSpeed = calAvgSpeed(this.lastSourcePoint, sourcePoint);
                    if (calAvgSpeed > Utils.DOUBLE_EPSILON) {
                        sourcePoint.setSpeed(calAvgSpeed);
                    }
                }
            }
        }
        this.lastSourcePoint = sourcePoint;
    }

    private double getMaxAcceleratedSpeed(double lastSpeed) {
        double d;
        int vSportType = RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING);
        double d2 = Utils.DOUBLE_EPSILON;
        if (vSportType == Workout.TYPE_WALKING) {
            d2 = 3.086419753d;
            d = 1.071673525d;
        } else if (vSportType == Workout.TYPE_RUNNING) {
            d2 = 3.703703704d;
            d = 1.714677641d;
        } else if (vSportType == Workout.TYPE_CYCLING) {
            d2 = 15.43209877d;
            d = 21.43347051d;
        } else {
            d = 0.0d;
        }
        return ((d2 * lastSpeed) - d) / Math.pow(lastSpeed, 2.0d);
    }

    private double getMaxValidSpeed() {
        int vSportType = RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING);
        double vSpeedMaxKph = 1000.0d;
        if (vSportType == Workout.TYPE_WALKING) {
            vSpeedMaxKph = 15.0d;
        } else if (vSportType == Workout.TYPE_RUNNING) {
            vSpeedMaxKph = 35.0d;
        } else if (vSportType == Workout.TYPE_CYCLING) {
            vSpeedMaxKph = 80.0d;
        }
        return h2s(vSpeedMaxKph);
    }

    private double calAvgSpeed(SourcePoint last, SourcePoint current) {
        long timestamp = current.getTimestamp() - last.getTimestamp();
        if (timestamp <= 0) {
            return Utils.DOUBLE_EPSILON;
        }
        double d = (DistanceUtil.get(last, current) * 1000.0d) / timestamp;
        if (d > getMaxValidSpeed()) {
            return last.getSpeed();
        }
        double pow = Math.pow(current.getSpeed(), 3.0d);
        double pow2 = Math.pow(d, 3.0d);
        double abs = Math.abs(pow - pow2) / (pow + pow2);
        return (current.getSpeed() * (1.0d - abs)) + (d * abs);
    }

    private boolean isSpeedValid(double lastSpeed, double accSpeed) {
        int i = RemoteSharedPreference.getInstance().getInt(SPConstant.KEY_SPORT_TYPE, Workout.TYPE_CYCLING);
        return i == Workout.TYPE_WALKING ? (lastSpeed <= Utils.DOUBLE_EPSILON || lastSpeed > h2s(4.0d) || accSpeed <= h2s(5.0d)) && ((lastSpeed <= h2s(4.0d) || lastSpeed > h2s(8.0d) || accSpeed <= h2s(3.0d)) && ((lastSpeed <= h2s(8.0d) || lastSpeed > h2s(10.0d) || accSpeed <= h2s(2.0d)) && (lastSpeed <= h2s(10.0d) || accSpeed <= h2s(1.0d)))) : i == 2 ? (lastSpeed <= Utils.DOUBLE_EPSILON || lastSpeed > h2s(6.0d) || accSpeed <= h2s(8.0d)) && ((lastSpeed <= h2s(6.0d) || lastSpeed > h2s(12.0d) || accSpeed <= h2s(5.0d)) && ((lastSpeed <= h2s(12.0d) || lastSpeed > h2s(16.0d) || accSpeed <= h2s(3.0d)) && (lastSpeed <= h2s(16.0d) || accSpeed <= h2s(2.0d)))) : (lastSpeed <= Utils.DOUBLE_EPSILON || lastSpeed > h2s(10.0d) || accSpeed <= h2s(15.0d)) && ((lastSpeed <= h2s(10.0d) || lastSpeed > h2s(20.0d) || accSpeed <= h2s(11.0d)) && ((lastSpeed <= h2s(20.0d) || lastSpeed > h2s(30.0d) || accSpeed <= h2s(8.0d)) && ((lastSpeed <= h2s(30.0d) || lastSpeed > h2s(40.0d) || accSpeed <= h2s(6.0d)) && ((lastSpeed <= h2s(40.0d) || lastSpeed > h2s(50.0d) || accSpeed <= h2s(4.0d)) && (lastSpeed <= h2s(50.0d) || accSpeed <= h2s(3.0d))))));
    }

    public void reset() {
        this.lastSourcePoint = null;
        this.errorTimes = 0;
    }
}
